package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.R;
import com.demo.dao.EmployeeDao;
import com.demo.domain.Employee;
import com.demo.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements EmployeeService {

    /**
     * 登录
     *
     * @param request
     * @param employee
     * @return * 处理逻辑如下:
     * 1、将页面提交的密码password进行md5加密处理
     * 2、根据页面提交的用户名username查询数据库
     * 3、如果没有查询到则返回登录失败结果
     * 4、密码比对，如果不一致则返回登录失败结果
     * 5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
     * 6、登录成功，将员工id存入Session并返回登录成功结果
     */
    @Override
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = this.getOne(lqw);

//        emp = this.getOne(Wrappers.<Employee>lambdaQuery().eq(Employee::getUsername,employee.getUsername()));
//        emp = this.lambdaQuery().eq(Employee::getUsername, employee.getUsername()).one();

        //3、如果没有查询到则返回登录失败结果
        //4、密码比对，如果不一致则返回登录失败结果
        if (emp == null || !emp.getPassword().equals(password)) {
            return R.error("账号或者密码错误");
        }

        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已封禁");
        }

        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出
     *
     * @param request
     * @return 1.将session中存储的id删除
     * 2.返回退出信息
     */
    @Override
    public R<String> logout(HttpServletRequest request) {
        //1.将session中存储的id删除
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 员工信息保存功能
     *
     * @param request
     * @param employee
     * @return
     */
    @Override
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());

        //设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        //设置新增时间与修改时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        //获取当前登录用户id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        this.save(employee);

        return R.success("新增成功");
    }

    /**
     * 分页查询所有员工数据
     *
     * @return
     */
    @Override
    public R<Page> page(int page, int pageSize, String name) {
        //打印获取到前端传来的数据
        log.info("前端传来的page = {} pageSize = {} name = {}", page, pageSize, name);

        //创建分页构造器
        Page pageInfo = new Page(page, pageSize);

        //创建查询构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //添加模糊查询name
        //判断name是否为空，为空就不添加模糊查询条件
        lqw.like(!StringUtils.isEmpty(name), Employee::getName, name);
        //添加排序条件，根据最新更新时间来排序
        lqw.orderByDesc(Employee::getUpdateTime);
//        List list = new ArrayList();
//        list.add(1);
        //notIn   表示不包含在内
//        lqw.notIn(Employee::getId,list); //也是将管理员信息隐藏

        //将管理员信息隐藏
        lqw.ne(Employee::getId,1);
        //开始查询

        this.page(pageInfo, lqw);
        return R.success(pageInfo);
    }

    /**
     * 修改员工信息
     *
     * @param request
     * @param employee
     * @return
     */
    @Override
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());

        //获取session中的用户id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        //更新修改时间和修改人
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        long id = Thread.currentThread().getId();
        log.info("当前线程id为 {}" ,id);

        this.updateById(employee);
        return R.success("修改成功！");
    }

    /**
     * 根据id回显数据
     *
     * @param id
     * @return
     */
    @Override
    public R<Employee> getById(@PathVariable Long id) {
        return R.success(this.lambdaQuery().eq(Employee::getId, id).one());
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @Override
    public R<String> delete(@PathVariable Long id) {
        if(removeById(id)){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }
}